#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

vector<int> P = {10,100,5,50};
int n = P.size();
vector<vector<long>> m(n+1, vector<long>(n+1, 999999));

long MatrixChain(vector<int> P, int n)
{
    for(int i = 1; i < n; i++)
    {
        m[i][i] = 0;
    }
    int r = 2;
    for(r=2;r<=n;r++)
    {
        for(int i=1;i<=n-r+1;i++)
        {
            int j=i+r-1;
            m[i][j] = m[i+1][j]+P[i-1]*P[i]*P[j];
            for(int k = i+1; k < j; k++)
            {
                int t = m[i][k]+m[k+1][j] + P[i-1]*P[k]*P[j];
                if(t<m[i][j])
                {
                    m[i][j] = t;
                }
            }
        }
    }
    return m[1][3];
}

int main()
{
    cout << MatrixChain(P, n);
}